//
//  HotCell.m
//  HappyTrip
//
//  Created by dlios on 15-3-24.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HotCell.h"
#import "UIImageView+WebCache.h"

@implementation HotCell
- (void)dealloc
{
    [_hotLabel1 release];
    [_hotLabel2 release];
    [_hotLabel3 release];
    [_hotLabel4 release];
    [_hotLabel5 release];
    [_titleLabel release];
    [_titleImage release];
    [_labelImage1 release];
    [_labelImage2 release];
    [_labelImage3 release];
    [_labelImage4 release];
    [_labelImage5 release];
    [_recommend release];
    [super dealloc];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLabel];
        
        self.titleImage = [[UIImageView alloc] init];
        [self.titleLabel addSubview:self.titleImage];
        [_titleImage release];
        [_titleLabel release];
        
        self.hotLabel1 = [[UILabel alloc] init];
        self.hotLabel1.layer.cornerRadius = 5;
        self.hotLabel1.clipsToBounds = YES;
        [self.contentView addSubview:self.hotLabel1];
        self.labelImage1 = [[UIImageView alloc] init];
        self.labelImage1.contentMode = UIViewContentModeScaleAspectFill;
        [self.hotLabel1 addSubview:self.labelImage1];
        [_labelImage1 release];
        [_hotLabel1 release];
        
        self.hotLabel2 = [[UILabel alloc] init];
        self.hotLabel2.layer.cornerRadius = 5;
        self.hotLabel2.clipsToBounds = YES;
        [self.contentView addSubview:self.hotLabel2];
        self.labelImage2 = [[UIImageView alloc] init];
        self.labelImage2.contentMode = UIViewContentModeScaleAspectFill;
        [self.hotLabel2 addSubview:self.labelImage2];
        [_labelImage2 release];
        [_hotLabel2 release];
        
        self.hotLabel3 = [[UILabel alloc] init];
        self.hotLabel3.layer.cornerRadius = 5;
        self.hotLabel3.clipsToBounds = YES;
        [self.contentView addSubview:self.hotLabel3];
        self.labelImage3 = [[UIImageView alloc] init];
        self.labelImage3.contentMode = UIViewContentModeScaleAspectFill;
        [self.hotLabel3 addSubview:self.labelImage3];
        [_labelImage3 release];
        [_hotLabel3 release];
        
        self.hotLabel4 = [[UILabel alloc] init];
        self.hotLabel4.layer.cornerRadius = 5;
        self.hotLabel4.clipsToBounds = YES;
        [self.contentView addSubview:self.hotLabel4];
        self.labelImage4 = [[UIImageView alloc] init];
        self.labelImage4.contentMode = UIViewContentModeScaleAspectFill;
        [self.hotLabel4 addSubview:self.labelImage4];
        [_labelImage4 release];
        [_hotLabel4 release];
        
        self.hotLabel5 = [[UILabel alloc] init];
        self.hotLabel5.layer.cornerRadius = 5;
        self.hotLabel5.clipsToBounds = YES;
        [self.contentView addSubview:self.hotLabel5];
        self.labelImage5 = [[UIImageView alloc] init];
        self.labelImage5.contentMode = UIViewContentModeScaleAspectFill;
        [self.hotLabel5 addSubview:self.labelImage5];
        [_labelImage5 release];
        [_hotLabel5 release];
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat width = self.contentView.bounds.size.width;
    CGFloat heigth = (self.contentView.bounds.size.height - 30) / 5;
        
    self.titleLabel.frame = CGRectMake(10, 0, width - 20, 30);

    self.titleLabel.text = [NSString stringWithFormat:@" %@", self.recommend.title];
    self.titleImage.frame = CGRectMake(0, 5, 3, 20);
    self.titleImage.image = [UIImage imageNamed:@"ticket_@2x.png"];
    
    NSArray *array = self.recommend.data;
//    NSLog(@"qiunile   %@", [[array objectAtIndex:4] objectForKey:@"first_day"]);
    
    self.hotLabel1.frame = CGRectMake(10, 35, width - 20, heigth - 5);
    self.labelImage1.frame = self.hotLabel1.bounds;
    NSString *str1 = [[self.recommend.data objectAtIndex:0] objectForKey:@"cover_image_w640"];
    NSURL *url1 = [NSURL URLWithString:str1];
    [self.labelImage1 setImageWithURL:url1 placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    
    self.hotLabel2.frame = CGRectMake(10, heigth + 35, width - 20, heigth - 5);
    self.labelImage2.frame = self.hotLabel2.bounds;
    NSString *str2 = [[self.recommend.data objectAtIndex:1] objectForKey:@"cover_image_w640"];
    NSURL *url2 = [NSURL URLWithString:str2];
    [self.labelImage2 setImageWithURL:url2 placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    
    self.hotLabel3.frame = CGRectMake(10, heigth * 2 + 35, width - 20, heigth - 5);
    self.labelImage3.frame = self.hotLabel3.bounds;
    NSString *str3 = [[self.recommend.data objectAtIndex:2] objectForKey:@"cover_image_w640"];
    NSURL *url3 = [NSURL URLWithString:str3];
    [self.labelImage3 setImageWithURL:url3 placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    
    self.hotLabel4.frame = CGRectMake(10, heigth * 3 + 35, width - 20, heigth - 5);
    self.labelImage4.frame = self.hotLabel4.bounds;
    NSString *str4 = [[self.recommend.data objectAtIndex:3] objectForKey:@"cover_image_w640"];
    NSURL *url4 = [NSURL URLWithString:str4];
    [self.labelImage4 setImageWithURL:url4 placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    
    self.hotLabel5.frame = CGRectMake(10, heigth * 4 + 35, width - 20, heigth - 5);
    self.labelImage5.frame = self.hotLabel5.bounds;
    NSString *str5 = [[self.recommend.data objectAtIndex:4] objectForKey:@"cover_image_w640"];
    NSURL *url5 = [NSURL URLWithString:str5];
    [self.labelImage5 setImageWithURL:url5 placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
}
- (UIImageView *)getView:(NSInteger)index
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.hotLabel1.bounds];
    NSString *str = [[self.recommend.data objectAtIndex:index] objectForKey:@"cover_image_w640"];
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    [imageView setImageWithURL:url];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, self.hotLabel1.frame.size.width - 20, 20)];
    label.text = [[self.recommend.data objectAtIndex:index] objectForKey:@"name"];
    label.textColor = [UIColor whiteColor];
    label.numberOfLines = 0;
    [label sizeToFit];
    [imageView addSubview:label];
    [label release];
    
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(10, label.frame.size.height + 5, 3, 20)];
    image.image = [UIImage imageNamed:@"ticket_@2x.png"];
    [imageView addSubview:image];
    [image release];
    
    
    UILabel *placeL = [[UILabel alloc] initWithFrame:CGRectMake(15, label.frame.size.height + 5, imageView.frame.size.width - 20, 10)];
    NSString *date = [[self.recommend.data objectAtIndex:index] objectForKey:@"first_day"];
    NSString *day = [[self.recommend.data objectAtIndex:index] objectForKey:@"day_count"];
    NSString *view_count = [[self.recommend.data objectAtIndex:index] objectForKey:@"view_count"];
    placeL.text = [NSString stringWithFormat:@"%@ %@天 %@ 次浏览", date, day, view_count];
    placeL.textColor = [UIColor whiteColor];
    placeL.font = [UIFont systemFontOfSize:10];
    [imageView addSubview:placeL];
    [placeL release];
    
    UILabel *popularL = [[UILabel alloc] initWithFrame:CGRectMake(15, label.frame.size.height + 15, imageView.frame.size.width - 20, 10)];
    popularL.text = [[self.recommend.data objectAtIndex:index] objectForKey:@"popular_place_str"];
    popularL.textColor = [UIColor whiteColor];
    popularL.font = [UIFont systemFontOfSize:10];
    [imageView addSubview:popularL];
    [popularL release];
    
    return [imageView autorelease];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
